<?php
/*
---------------------------------------------------------------------------
|
|   Viper Cart
|   --------------------------------------------------
|   Copyright (c) 2006 - 2007 Craig Brass Systems
|
|   Phone: 0845 094 6310
|   Fax: 0845 094 6320
|   Web: http://www.craigbrasssystems.com
|   E-Mail: admin@craigbrasssystems.com
|   Address: Craig Brass Systems, 43b Millbrook Road,
|              Kingstown Industrial Estate, Carlisle, Cumbria, CA3 0EU,
|                United Kingdom
|
---------------------------------------------------------------------------
|
|   Viper Cart is NOT free software. Please see the following licence 
|     agreement for more details :-
|   License Agreement: http://www.vipercart.com/go/license_agreement
|
---------------------------------------------------------------------------
|
|   Revision Number: 1
|   Date and Time of Creation: 08/07/07 @ 11:30 (London Time)
|   Date and Time of Last Revision: 08/07/07 @ 11:30 (London Time)
|   Last Revision by: andrewgillard
|
---------------------------------------------------------------------------
|
|   Title of File: Kayako LoginShare File
|   Details of File: This file is a file included with the Kayako software
|                      packages that allows basic integration between the
|                        two pieces of software.
|
---------------------------------------------------------------------------
*/

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Initialization function. You can connect to your database etc over here.
*/
function loginShareInit()
{
	global $loginshare;

	$loginshare->moduleloaded = true;
}

/**
* Authorize a user based on email and password
*/
function loginShareAuthorize($username, $password)
{
	global $dbCore, $_SWIFT, $loginshare, $settings;

	$_loginshare = $settings->getSection("loginshare");
	$dbLoginShare = new dbCore($_loginshare["vchostname"], $_loginshare["vcdbuser"], $_loginshare["vcdbpass"], $_loginshare["vcdbname"], "mysql", false);

	$_user = $dbLoginShare->queryFetch("SELECT * FROM `". $_loginshare["vcdbprefix"] ."customers` WHERE `email` = '". $dbCore->escape($username) ."';");
	if (empty($_user["id"]))
	{
		return false;
	}

	$userpassword = md5($password);
	
	$regpassword = substr(buildHash(),0,8);

	if ($_user["password"] == $userpassword && !empty($_user["password"]))
	{		
		// We have a match, Seems like a valid user.. now see if he is registered..
		$userid = getLoginShareUser(LOGINAPI_VIPERCART, $_user["id"]);
		if (!$userid)
		{
			// Not registered, Register him
			$userid = insertUser(true, $_user["email"], $regpassword, $_SWIFT["tgroup"]["regusergroupid"], LOGINAPI_VIPERCART, $_user["id"], $_user["firstname"]." ".$_user["lastname"], $_SWIFT["tgroup"]["languageid"], 0, false, 1, true);
		}

		if (!$userid)
		{
			return false;
		}

		$_swiftuser = $loginshare->loadSWIFTUser($userid);
		if (!$_swiftuser)
		{
			return false;
		}

		$_SWIFT["user"] = $_swiftuser;

		return $_swiftuser["userid"];
	}

	return false;
}

/**
* Return the Unique User ID of the current user
*/
function loginShareUserID()
{
	global $_SWIFT;

	if (empty($_SWIFT["user"]["userid"]))
	{
		return false;
	} else {
		return $_SWIFT["user"]["userid"];
	}
}

/**
* Logout the current user
*/
function loginShareLogout()
{
	global $session, $_SWIFT;

	$session->updateSession($_SWIFT["session"]["sessionid"], 0);

	return true;
}

/**
* Load the user credentials into current workspace. The following variables should be declared for proper working:
* userid - User id that is set in the "users" table
* fullname
* email - Array
* password (MD5 Hashed)
* usergroupid - If this is not set, then it will use the default registered user group for this template group
*/
function loginShareLoadUser()
{
	global $dbCore, $_SWIFT, $loginshare;

	if (empty($_SWIFT["session"]["typeid"]))
	{
		$_SWIFT["user"]["loggedin"] = false;
		return false;
	}

	$_user = $loginshare->loadSWIFTUser($_SWIFT["session"]["typeid"]);
	if (!$_user)
	{
		$_SWIFT["user"]["loggedin"] = false;

		return false;
	}

	$_SWIFT["user"] = $_user;

	return true;
}

/**
* Renders the Login Share Form
*/
function renderLoginShareForm()
{
	global $_SWIFT;

	$forms = array();

	$forms[0]["title"] = $_SWIFT["language"]["hostname"];
	$forms[0]["name"] = "vchostname";
	$forms[1]["title"] = $_SWIFT["language"]["dbname"];
	$forms[1]["name"] = "vcdbname";
	$forms[2]["title"] = $_SWIFT["language"]["dbuser"];
	$forms[2]["name"] = "vcdbuser";
	$forms[3]["title"] = $_SWIFT["language"]["dbpass"];
	$forms[3]["name"] = "vcdbpass";
	$forms[4]["title"] = "DB Table Prefix";
	$forms[4]["name"] = "vcdbprefix";

	return $forms;

}
?>
